package com.cn.mmd.controller;

import com.cn.mmd.model.Information;
import com.cn.mmd.model.StudentApp;
import com.cn.mmd.model.StudentInformation;
import com.cn.mmd.model.TeacherStatistics;
import com.cn.mmd.service.InformationService;
import com.cn.mmd.service.StudentAppService;
import com.cn.mmd.service.TeacherService;
import com.cn.mmd.tools.GetLimitNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/22 0022.
 */
@Controller
@RequestMapping(value = "page/stuInformation")
public class StudentInformationController {

    @Autowired
    private InformationService informationService;

    @Autowired
    private StudentAppService studentAppService;

    @Autowired
    private TeacherService teacherService;

    private GetLimitNum getLimitNum = new GetLimitNum();

    @Autowired
    private TokenController tokenController;

    @RequestMapping(value = "/select")
    @ResponseBody public synchronized Object stuSelectInformation(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String,Object> map;
        int startrow = Integer.parseInt(request.getParameter("startrow"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        if (startrow<1){
            startrow=1;
        }
        startrow = (startrow - 1) * pageSize;
        String informationName = request.getParameter("informationName");
        Information information = new Information(startrow,pageSize,informationName);
        String studentId = request.getParameter("studentId");
        String token = request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp != null) {
            List<Information> informationList = informationService.selectLimitBy(information);
            for (int i = 0; i < informationList.size(); i++) {
                information = informationList.get(i);
                map = new HashMap<String, Object>();
                map.put("informationId", information.getInformationid() + "");
                map.put("informationName", information.getInformationname());
                map.put("publisherName", information.getPublisherid());
                map.put("releaseTime", information.getReleasetime());
//            map.put("downloads",information.getDownloads());
                map.put("readNumber", information.getReadnumber() + "");
                map.put("informationUrl", information.getInformationurl());
                map.put("informationContent", information.getInformationcontent());
                mapList.add(map);
            }
            if (mapList != null && mapList.size() > 0) {
                information = new Information();
                information.setInformationname(informationName);
                int i1 = informationService.countLimitNum(information);
                objectMap.put("total", i1);
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
                objectMap.put("data", mapList);
            } else {
                objectMap.put("code", 201);
                objectMap.put("data", mapList);
                information = new Information();
                information.setInformationname(informationName);
                int i1 = informationService.countLimitNum(information);
                objectMap.put("total", i1);
                objectMap.put("message", "没有资料");
            }
        }else {
            objectMap.put("code", 202);
            information = new Information();
            information.setInformationname(informationName);
            int i1 = informationService.countLimitNum(information);
            objectMap.put("total", i1);
            objectMap.put("message", "此用户没有权限做此操作");
        }
        return objectMap;
    }

    @RequestMapping(value = "/selectOne")
    @ResponseBody public synchronized Object stuSelectInformationOne(HttpServletRequest request) {
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("studentId");
        Integer informationId = Integer.parseInt(request.getParameter("informationId"));
        String token = request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp != null) {
            StudentInformation studentInformation = new StudentInformation();
            studentInformation.setStuid(studentId);
            studentInformation.setInformationid(informationId);
            studentInformation.setInformationtype("阅读");
            if (informationService.selectByAll(studentInformation) == null) {
                if (informationService.insertSelective(studentInformation) > 0) {
                    Information information;
                    information = informationService.selectByPrimaryKey(informationId);
                    TeacherStatistics teacherStatistics = teacherService.selectByPrimaryKey(information.getPublisherid());
                    int num1 = 1 + teacherStatistics.getBeviewed();
                    teacherStatistics = new TeacherStatistics();
                    teacherStatistics.setTeacherid(information.getPublisherid());
                    teacherStatistics.setBeviewed(num1);
                    teacherService.updateByPrimaryKeySelective(teacherStatistics);
                    int num = 1 + studentApp.getViewednum();
                    studentApp = new StudentApp();
                    studentApp.setViewednum(num);
                    studentApp.setStudentid(studentId);
                    studentAppService.updateByPrimaryKeySelective(studentApp);
                    objectMap.put("code", 200);
                    objectMap.put("message", "阅读次数加1");
                } else {
                    objectMap.put("code", 201);
                    objectMap.put("message", "添加次数失败");
                }
            } else {
                objectMap.put("code", 202);
                objectMap.put("message", "重复不添加阅读次数");
            }
        }else {
            objectMap.put("code", 203);
            objectMap.put("message", "此用户没有权限做此操作");
        }
        return objectMap;
    }
}
